﻿// -----------------------------------------------------------------------
// <copyright file="LocatorBase.cs" company="CompDJ">
//     Copyright (c) CompDJ. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace T4DemoCs.ViewModel
{
    using GalaSoft.MvvmLight;

    /// <summary>Locator Base class.</summary>
    /// <typeparam name="TViewModel">View model</typeparam>
    public abstract class LocatorBase<TViewModel> : ObservableObject
    {
        /// <summary>RuntimeViewModel backing field</summary>
        private TViewModel runtimeViewModel;

        /// <summary>DesignTimeViewModel backing field</summary>
        private TViewModel designTimeViewModel;

        /// <summary>Gets a value indicating whether the locator is in design mode.</summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes", 
            Justification = "Needed by static classes")]
        public static bool IsInDesignMode
        {
            get
            {
                return GalaSoft.MvvmLight.ViewModelBase.IsInDesignModeStatic;
            }
        }

        /// <summary>Gets current ViewModel instance so if we are in designer its <see cref="DesigntimeViewModel"/> and if its runtime then its <see cref="RuntimeViewModel"/>.</summary>
        public TViewModel ViewModel
        {
            get
            {
                return IsInDesignMode ? this.DesignTimeViewModel : this.RuntimeViewModel;
            }
        }

        /// <summary>Gets or sets the DesigntimeViewModel property.</summary>
        /// <remarks>Holds the intance of the designtime version of the ViewModel that is instantiated only when application is opened in IDE designer (VisualStudio, Blend etc).</remarks>
        public TViewModel DesignTimeViewModel
        {
            get
            {
                return this.designTimeViewModel;
            }

            set
            {
                this.Set<TViewModel>("ViewModel", ref this.designTimeViewModel, value);
            }
        }

        /// <summary>Gets or sets the intance of the runtime version of the ViewModel that is instantiated only when application is really running by retrieving the instance from IOC container</summary>
        protected TViewModel RuntimeViewModel
        {
            get
            {
                if (this.runtimeViewModel == null)
                {
                    this.RuntimeViewModel = Bootstrapper.Resolve<TViewModel>();
                }

                return this.runtimeViewModel;
            }

            set
            {
                this.Set<TViewModel>("ViewModel", ref this.runtimeViewModel, value);
            }
        }
    }
}